package com.sx.zongzhi.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.ServicerSelectAdapter;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.ServicerBean;
import com.sx.zongzhi.presenter.ServicerPest;
import com.sx.zongzhi.utils.Global;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IServicerView;
import com.sx.zongzhi.widget.ClearEditText;
import com.sx.zongzhi.widget.LoadDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 服务人员选择
 */
public class ServicerSelectActivity extends BaseActivity implements IServicerView{

    @BindView(R.id.et_search)
    ClearEditText et_search;
    @BindView(R.id.list_View)
    ListView list_View;
    private String currentActivity;
    private List<ServicerBean.RowsBean> servicerBeen = new ArrayList<>();
    private ServicerPest servicerPest;
    private ServicerSelectAdapter selectAdapter;
    private String content="";

    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_servicer_select);
        setTitleName("服务人员选择");
        ButterKnife.bind(this);
        currentActivity=getIntent().getStringExtra(Global.ACTIVITY_FRAGMENT);
        initData();
    }

    private void initData() {
        servicerPest=new ServicerPest(this);
        LoadDialog.show(this);
        servicerPest.getServicerList(content);
        et_search.addTextChangedListener(textWatcher);
    }

    TextWatcher textWatcher=new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {}
        @Override
        public void afterTextChanged(Editable s) {
            content = s.toString().trim();
            initData();
        }
    };




    @Override
    public void showError(String message) {
        LoadDialog.show(this);
        ToastUtils.showToast(this,message);
    }

    @Override
    public void getServicerList(ServicerBean servicerBeanList) {
        LoadDialog.dismiss(this);
        if (servicerBeanList != null && servicerBeanList.getRows() != null ){
            servicerBeen.clear();
            servicerBeen.addAll(servicerBeanList.getRows());
            if (selectAdapter==null){
                selectAdapter = new ServicerSelectAdapter(this,R.layout.item_contact_list,servicerBeen);
                list_View.setAdapter(selectAdapter);
                list_View.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        MyRxBus.get().post(new MsgEvent<>(currentActivity,0,servicerBeen.get(position)));
                        finish();
                    }
                });

            }else {
                selectAdapter.notifyDataSetChanged();
            }
        }

    }
}
